#include "StdAfx.h"
#include "UIMenu.h"


#if   defined (_MSC_VER)  && (_MSC_VER <1300) 
#include <multimon.h>    // DUIPopupMenuWindow.cpp have defined the STUB
#else
#if WINVER < 0x0500
#define COMPILE_MULTIMON_STUBS
#include <multimon.h>
#endif // WINVER < 0x0500
#endif

#define  DUI_MENU_PROP_FALG  _T("dui_menu_flag")
#define  DUI_MENU_PROP_VALUE 314

#define MENU_TEXT_HOTKEY_GAP 10 //gap between "abc Ctrl+A"
#define DUI_MENU_CHILD_GAP 2
#define DUI_WND_CLASS _T("DUI_MenuWnd")

#define  UIEVENT_MENUITEM_MOUSEENTER UIEVENT__LAST +1
#define  UIEVENT_MENUITEM_MOUSELEAVE UIEVENT__LAST +2
#define  UIEVENT_MENUITEM_CLICK      UIEVENT__LAST +3

namespace DuiLib{

	BOOL IsDUIMenuWnd(HWND hWnd)
	{
		if(hWnd == NULL || !IsWindow(hWnd)) return FALSE;

		TCHAR szClassName[32] = {0};
		GetClassName(hWnd, szClassName, sizeof(szClassName) / sizeof(TCHAR) - 1);
		if(lstrcmpi(szClassName, DUI_WND_CLASS) == 0)
		{
			HANDLE hProp = GetProp(hWnd, DUI_MENU_PROP_FALG);
			if(hProp == (HANDLE)DUI_MENU_PROP_VALUE)
			{
				return TRUE;
			}
		}

		return FALSE;
	}


	CDUIMenuItem* g_pMenuItemClicked = NULL;

	class CDUIMenuWnd: public CWindowWnd
	{
	public:
		CDUIMenuWnd(CDUIMenu*);
		LRESULT OnCreate(UINT, WPARAM, LPARAM, BOOL& bHandled);
		LRESULT OnDestroy(UINT, WPARAM, LPARAM, BOOL& bHandled);
		LRESULT OnSetFocus(UINT, WPARAM, LPARAM, BOOL& bHandled);
		LRESULT OnKillFocus(UINT, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	    LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
		LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
		virtual LPCTSTR GetWindowClassName() const;
		HWND CreateFromRootControl(CControlUI * pRootControl, 
			HWND hWndParent, RECT& rcPos, LPCTSTR szWindowName = NULL,
			DWORD dwStyle = 0, DWORD dwExStyle = 0,
			UINT nID = 0, LPVOID lpCreateParam = NULL);
		CPaintManagerUI * GetPaintManager(); 
		void GetWindowRect(RECT* rect);
		void AdjustLocation(RECT rc);
	protected:
		CDUIMenu* m_pMenu;
	private:
		bool m_bfisrt;
		CPaintManagerUI m_pm;
	};

	CDUIMenuWnd::CDUIMenuWnd(CDUIMenu* pMenu)
	{
		_ASSERT(pMenu != NULL);
		m_bfisrt = true;
		m_pMenu = pMenu;
	}

	LRESULT CDUIMenuWnd::OnCreate(UINT, WPARAM, LPARAM, BOOL& bHandled)
	{
		bHandled = FALSE;

		m_pm.Init(m_hWnd);
		m_pm.SetBackgroundTransparent(true);
		::SetProp(m_hWnd, DUI_MENU_PROP_FALG, (HANDLE)DUI_MENU_PROP_VALUE);
		return S_OK;
	}


	LRESULT CDUIMenuWnd::OnDestroy(UINT, WPARAM, LPARAM, BOOL& bHandled)
	{
		bHandled = TRUE;

		::RemoveProp(m_hWnd, DUI_MENU_PROP_FALG);

		//for ::SendMessage(hWndParent, WM_NCACTIVATE, TRUE, 0); in ShowMenuWnd()
		HWND hWndOwner = ::GetWindow(m_hWnd, GW_OWNER);
		if(hWndOwner != NULL
			&& GetActiveWindow() != hWndOwner
			&& !IsDUIMenuWnd(hWndOwner))
		{
			::SendMessage(hWndOwner, WM_NCACTIVATE, FALSE, 0);
		}
		return 1;
	}

	LRESULT CDUIMenuWnd::OnSetFocus(UINT, WPARAM, LPARAM, BOOL& bHandled)
	{
		bHandled = FALSE;

		if(m_pMenu != NULL)
		{
			m_pMenu->SetFocus();
		}

		return S_OK;
	}

	LRESULT CDUIMenuWnd::OnKillFocus(UINT, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		bHandled = FALSE;

		HWND hWnd = (HWND)wParam;
		if(!IsDUIMenuWnd(hWnd))
		{
			if(m_pMenu != NULL)
			{
				m_pMenu->CancelMenu(TRUE);
			}
		}

		return S_OK;
	}

	LRESULT CDUIMenuWnd::OnSize( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
	{
		SIZE szRoundCorner = m_pm.GetRoundCorner();
#if defined(WIN32) && !defined(UNDER_CE)
		if( !::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0) ) {
			CDuiRect rcWnd;
			::GetWindowRect(*this, &rcWnd);
			rcWnd.Offset(-rcWnd.left, -rcWnd.top);
			rcWnd.right++; rcWnd.bottom++;
			HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
			::SetWindowRgn(*this, hRgn, TRUE);
			::DeleteObject(hRgn);
		}
#endif
		bHandled = FALSE;
		return 0;
	}
	LRESULT CDUIMenuWnd::HandleMessage( UINT uMsg, WPARAM wParam, LPARAM lParam )
	{
		LRESULT lRes = 0;
		BOOL bHandled = TRUE;
		switch( uMsg ) 
		{
		case WM_CREATE:        lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
		case WM_CLOSE:         return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
		case WM_DESTROY:     lRes = OnDestroy(uMsg, wParam, lParam, bHandled); break;
		case WM_SETFOCUS:       lRes =OnSetFocus(uMsg,wParam,lParam,bHandled);break;
		case WM_KILLFOCUS:    lRes = OnKillFocus(uMsg, wParam, lParam, bHandled); break;
		case WM_SIZE:          lRes = OnSize(uMsg,wParam,lParam,bHandled);break;
		default:
			bHandled = FALSE;
		}

		if( bHandled ) return lRes;
		if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes;
		return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
	}

	HWND CDUIMenuWnd::CreateFromRootControl(CControlUI* pRootControl, HWND hWndParent, RECT& rcPos, LPCTSTR szWindowName /*= NULL*/,
		DWORD dwStyle/* = 0*/, DWORD dwExStyle/* = 0*/,
		UINT nID/* = 0*/, LPVOID lpCreateParam/* = NULL*/)
	{
		if(IsWindow()) return NULL;

		_ASSERT(pRootControl);
		if(pRootControl==NULL) return NULL;

		
		HWND hwnd =  Create(hWndParent, szWindowName, 
			dwStyle,
			dwExStyle, rcPos.left, rcPos.top, rcPos.right,rcPos.bottom);
		if(m_bfisrt)
		{
			m_pm.AttachDialog(pRootControl);
			m_bfisrt = false;
		}

		return hwnd;
		//return CreateWindow(hWndParent, rcPos, szWindowName, dwStyle, dwExStyle, nID, lpCreateParam);
	}

	LPCTSTR CDUIMenuWnd::GetWindowClassName() const
	{
		return DUI_WND_CLASS;
	}

    CPaintManagerUI * CDUIMenuWnd::GetPaintManager()
	{
		return &m_pm;
	}

	void CDUIMenuWnd::GetWindowRect( RECT* rect )
	{
		::GetWindowRect(m_pm.GetPaintWindow(),rect);
	}

	void CDUIMenuWnd::AdjustLocation( RECT rc )
	{
		::SetWindowPos(m_pm.GetPaintWindow(),HWND_TOP,rc.left,rc.top,rc.right-rc.left,rc.bottom-rc.top,SWP_NOZORDER);
	}



	////////////////////  CDUIMenuItem  /////////////////////////
	CDUIMenuItem::CDUIMenuItem()
	{
		m_dwUserData = 0;
		m_bSelect = FALSE;
		m_dwMenuStyle = 0;
		m_ChildMenu = NULL;
		m_uTextStyle=DT_VCENTER;
		m_dwStatus = 0;
		m_dwHotTextColor = -1;
	}

	LPVOID CDUIMenuItem::GetInterface(LPCTSTR strName)
	{
		if(_tcscmp(strName,DUI_CTR_MENUITEM) == 0)
		{
			return (CDUIMenuItem*)this;
		}

		return CLabelUI::GetInterface(strName);
	}


	VOID CDUIMenuItem::SetControlRect(const RECT& rt)
	{
		//m_rtControl = rt;

		//UpdateLayout(FALSE);
	}	


	void CDUIMenuItem::DoEvent(TEventUI& event)
	{
		BOOL bHandled = FALSE;
		switch(event.Type)
		{
		case UIEVENT_MOUSEENTER:
			{
				ModifyStatus(0, UISTATE_HOT);	
				TEventUI newevent = event;
				newevent.pSender = this;
				newevent.Type = UIEVENT_MENUITEM_MOUSEENTER;

				m_pParent->DoEvent(newevent);
				bHandled = TRUE;
			}

			break;

		case UIEVENT_MOUSELEAVE:
			{
				ModifyStatus(UISTATE_HOT, 0);
				TEventUI newevent = event;
				newevent.pSender = this;
				newevent.Type = UIEVENT_MENUITEM_MOUSELEAVE;
				m_pParent->DoEvent(newevent);
				bHandled = TRUE;
			}
	
			break;

		case UIEVENT_BUTTONUP:
			ModifyStatus(UISTATE_PUSHED, 0);
			if(PtInRect(&m_rcItem, event.ptMouse))
			{
				if(m_pParent != NULL)
				{
					TEventUI newevent = event;
					newevent.pSender = this;
					newevent.Type = UIEVENT_MENUITEM_CLICK;
					m_pParent->DoEvent(newevent);
				}
			}
			bHandled = TRUE;
			break;
		default:
			break;
		}

		if(bHandled) return;
		if(m_pParent!=NULL)
			m_pParent->DoEvent(event);
		else
			CControlUI::DoEvent(event);
	}

	DWORD CDUIMenuItem::ParseMenuItemStyle(const CDuiString& strStyle)
	{
		DWORD dwStyle(0);
		if(strStyle.Find(_T("MF_STRING")) != -1)
		{
			dwStyle |= MF_STRING;
		}

		if(strStyle.Find(_T("MF_SEPARATOR")) != -1)
		{
			dwStyle &= ~MF_STRING;
			dwStyle |= MF_SEPARATOR;
		}

		if(strStyle.Find(_T("MF_POPUP")) != -1)
		{
			dwStyle |= MF_POPUP;
		}

		if(strStyle.Find(_T("MF_CHECKED")) != -1)
		{
			dwStyle |= MF_CHECKED;
		}

		if(strStyle.Find(_T("MF_GRAYED")) != -1)
		{
			dwStyle |= MF_GRAYED;
		}

		if(strStyle.Find(_T("MF_DISABLED")) != -1)
		{
			dwStyle |= MF_DISABLED;
		}

		return dwStyle;
	}

	VOID CDUIMenuItem::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
	{
		BOOL bHandled(FALSE);

		if(_tcscmp(pstrName,_T("textcolor")) == 0)
		{
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetTextColor(clrColor);

			bHandled = TRUE;
		}
		else if(_tcscmp(pstrName,_T("style")) == 0)
		{
			SetMenuItemStyle(ParseMenuItemStyle(pstrValue));
			bHandled = TRUE;
		}
		else if(_tcscmp(pstrName,_T("hottextcolor")) == 0)
		{
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetHotTextColor(clrColor);
			bHandled = TRUE;
		}
		else if (_tcscmp(pstrName,_T("icon")) == 0)
		{
			SetIcon(pstrValue);
			bHandled = TRUE;
		}

		if(!bHandled)
		{
			CControlUI::SetAttribute(pstrName, pstrValue);
		}
	}


	VOID CDUIMenuItem::SetMenuItemStyle(DWORD dwStyle) 
	{
		m_dwMenuStyle = dwStyle;
	}

	DWORD CDUIMenuItem::GetMenuItemStyle() const 
	{
		return m_dwMenuStyle;
	}


	VOID CDUIMenuItem::SetSelect(BOOL bSelect) 
	{
		m_bSelect = bSelect;
	}

	BOOL CDUIMenuItem::GetSelect() const 
	{
		return m_bSelect;
	}



	VOID CDUIMenuItem::SetItemData(DWORD dwData) 
	{
		m_dwUserData = dwData;
	}

	DWORD CDUIMenuItem::GetItemData() const
	{
		return m_dwUserData;
	}

	VOID CDUIMenuItem::SetChildMenu( CDUIMenu * pChildMenu )
	{
		m_ChildMenu = pChildMenu;
	}

	CDUIMenu* CDUIMenuItem::GetChildMenu() const
	{
		if(GetMenuItemStyle()&MF_POPUP)
		{
			if(GetChildControlCount() > 0)
			{
				_ASSERT(GetChildControlCount() <= 1);
				CDUIMenu* pMenu = (CDUIMenu*)GetChildControlByIndex(0)->GetInterface(DUI_CTR_MENU);
				_ASSERT(pMenu != NULL);
				return pMenu;
			}
		}

		return m_ChildMenu;
	}

	// 	VOID CDUIMenuItem::SetText(const CDUIString& strText)
	// 	{
	// 		theBase::SetText(stringReplace(strText, _T("\\t"), _T("\t")));
	// 	}

	INT CDUIMenuItem::GetIdealWidth()
	{
		INT nRet(0);
		CDUIMenu* pMenu = GetMenuOwner();
		if(pMenu != NULL)
		{
			//nRet += pMenu->GetIconRectWidth();
			RECT rc = pMenu->GetTextPadding();
			nRet += (rc.left +rc.right);
			//nRet += pMenu->GetArrowRectWidth();
			nRet += GetPaddingWidth() * 2;

		}

		if(m_sText.GetLength()> 0 )
		{
			BOOL bHotKey = TRUE;
			if(bHotKey) nRet += MENU_TEXT_HOTKEY_GAP;
			HDC dc =::GetDC(pMenu->GetOwerWnd()->GetHWND());
			SIZE sizeText = CRenderEngine::GetTextSize(dc,pMenu->GetManager(),m_sText, pMenu->GetFont(), DT_LEFT | DT_SINGLELINE | DT_CALCRECT);
			nRet += sizeText.cx;
		}

		return nRet;
	}

	RECT CDUIMenuItem::GetBKRect()
	{
		return m_rcItem;
	}

	RECT CDUIMenuItem::GetIconRect()
	{
		RECT rtRet = {0, 0, 0, 0};
		CDUIMenu* pMenu = static_cast<CDUIMenu*>(GetMenuOwner()) ;
		if(pMenu)
		{
			return  pMenu->GetIconRect();
		}
		return rtRet;
	}

	RECT CDUIMenuItem::GetTextRect()
	{
		RECT rtIcon = GetIconRect();
		RECT rtArrow = GetArrowRect();
		RECT rtBK = GetBKRect();
		rtBK.left = rtIcon.right;
		rtBK.right = rtArrow.left;
		rtBK.left += GetPaddingWidth();
		rtBK.right -= GetPaddingWidth();
		if(IsRectEmpty(&rtBK)) 
		{
			rtBK.right = rtBK.left;
		}
		return rtBK;
	}

	RECT CDUIMenuItem::GetArrowRect()
	{
		RECT rtRet = {0, 0, 0, 0};
		CDUIMenu* pMenu = GetMenuOwner();
		if(pMenu)
		{
			RECT rtBK = GetBKRect();
			rtRet = pMenu->GetArrowRect();
			rtRet.left = rtBK.right - rtRet.left;	
		}
		return rtRet;
	}


	BOOL CDUIMenuItem::IsMenuItemEnable()
	{
		if(!IsEnabled()) return FALSE;
		if((m_dwMenuStyle & MF_GRAYED)
			|| (m_dwMenuStyle & MF_DISABLED)
			|| (m_dwMenuStyle & MF_SEPARATOR))
		{
			return FALSE;
		}

		return TRUE;
	}

	BOOL CDUIMenuItem::BeforeAddControl(CDUIMenuItem* pItem)
	{
		if(pItem == NULL) return FALSE;
		for(int i = 0;i<m_childitems.GetSize();i++)
		{
			if(m_childitems[i]== pItem)
			{
				return FALSE;
			}
		}
		pItem->SetParent(this);
		CDUIMenuItem* pMenuItem = (CDUIMenuItem*)pItem->GetInterface(DUI_CTR_MENU);
		_ASSERT(pMenuItem != NULL);
		if(pMenuItem == NULL) return FALSE;

		return TRUE;	
	}

	VOID CDUIMenuItem::PaintBkgnd(HDC dc)
	{
		if(m_dwStatus&UISTATE_HOT)
		{
			CDUIMenu * pmenu = GetMenuOwner();
			ASSERT(pmenu);
			CDuiString strimage = pmenu->GetHotMenuItemImage();
			if(strimage.IsEmpty() ) return;

			DUITRACE(GetText());
			if( !DrawImage(dc, (LPCTSTR)strimage) ) pmenu->SetHotMenuItemImage(_T(""));
		}
	}

 	VOID CDUIMenuItem::PaintForgnd(HDC dc)
 	{
		CDUIMenu * pMenu = static_cast<CDUIMenu*>(GetMenuOwner());
		ASSERT(pMenu);
		if(!m_sIconImage.IsEmpty())
		{
 			RECT rc = pMenu->GetIconRect();
			rc.left += m_rcPaint.left;
			rc.top  += m_rcPaint.top;
			rc.right += rc.left;
			rc.bottom += rc.top;
			CRenderEngine::DrawImageString(dc, m_pManager, rc, m_rcPaint, (LPCTSTR)m_sIconImage, NULL);

		}
		if(m_ChildMenu != NULL)
		{
			RECT rc = pMenu->GetArrowRect();
			rc.left = m_rcPaint.right- rc.left;
			rc.top  += m_rcPaint.top;
			rc.right += rc.left;
			rc.bottom += rc.top;
			CRenderEngine::DrawImageString(dc, m_pManager, rc, m_rcPaint,pMenu->GetArrowImage(), NULL);
		}
 	}

	VOID CDUIMenuItem::PaintText(HDC hDC)
	{
		if( IsFocused() ) m_uItemState |= UISTATE_FOCUSED;
		else m_uItemState &= ~ UISTATE_FOCUSED;
		if( !IsEnabled() ) m_uItemState |= UISTATE_DISABLED;
		else m_uItemState &= ~ UISTATE_DISABLED;

		if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
		if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();

		if( m_sText.IsEmpty() ) return;
		int nLinks = 0;


		DWORD clrColor = IsEnabled()?m_dwTextColor:m_dwDisabledTextColor;

		RECT rc = m_rcItem;
		CDUIMenu * pMenu = static_cast<CDUIMenu*>(m_pParent->GetInterface(DUI_CTR_MENU));
		if(pMenu != NULL)
		{
			RECT rcpading = pMenu->GetTextPadding();
			rc.left += rcpading.left;
			rc.right -= rcpading.right;
		}

		if(m_dwStatus&UISTATE_HOT)
		{
			if(m_dwHotTextColor == -1)
			{
				if(pMenu != NULL)
					m_dwHotTextColor =  pMenu->GetHotTextColor();
				else
					m_dwHotTextColor = m_dwTextColor;
			}
			clrColor = m_dwHotTextColor;
		}


		if(m_iFont == -1)
		{
			CDUIMenu *pMenu = GetMenuOwner();
			ASSERT(pMenu != NULL);
			m_iFont = pMenu->GetFont();
		}
		CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, clrColor, \
			m_iFont, DT_VCENTER|DT_SINGLELINE);

	}

	CDUIMenu* CDUIMenuItem::GetMenuOwner()
	{
		if(m_pParent != NULL)
		{
			return (CDUIMenu*)m_pParent->GetInterface(DUI_CTR_MENU);
		}
		return NULL;
	}

	VOID CDUIMenuItem::ModifyStatus( DWORD dwRemove, DWORD dwAdd )
	{
		DWORD dwNewStatus = (m_dwStatus & ~dwRemove) | dwAdd;
		if(m_dwStatus != dwNewStatus)
		{
			m_dwStatus = dwNewStatus;

		}
		//Invalidate();
	}

	INT CDUIMenuItem::GetChildControlCount() const
	{
		return m_childitems.GetSize();
	}

	CControlUI * CDUIMenuItem::GetChildControlByIndex( const int index ) const
	{
		if( index < 0 || index >= m_childitems.GetSize() ) return NULL;
		return static_cast<CControlUI*>(m_childitems[index]);
	}

	int CDUIMenuItem::GetPaddingWidth()
	{
		RECT rc = GetPadding();
		return (rc.right-rc.left);
	}

	void CDUIMenuItem::SetTextColor( DWORD _TextColor )
	{
		m_dwTextColor = _TextColor;
	}

	DWORD CDUIMenuItem::GetTextColor() const
	{
		return m_dwTextColor;
	}

	void CDUIMenuItem::DoPaint( HDC hDC, const RECT& rcPaint )
	{
		if( !::IntersectRect(&m_rcPaint, &rcPaint, &m_rcItem) ) return;
		PaintBkgnd(hDC);
		PaintText(hDC);
		PaintForgnd(hDC);
	}

	VOID CDUIMenuItem::SetIcon( LPCTSTR pIcon )
	{
		m_sIconImage = pIcon;
	}

	LPCTSTR CDUIMenuItem::GetIcon() const
	{
		return (LPCTSTR)m_sIconImage;
	}

	void CDUIMenuItem::SetHotTextColor( DWORD TextColor )
	{
		m_dwHotTextColor = TextColor;
	}

	DWORD CDUIMenuItem::GetHotTextColor() const
	{
		return m_dwHotTextColor;
	}


	////////////////////  CDUIMenu  /////////////////////////
	CDUIMenu::CDUIMenu()
	{
		memset(&m_nIconRect,0,sizeof(m_nIconRect));
		memset(&m_nArrowRect,0,sizeof(m_nArrowRect));

		m_pMenuWnd = new CDUIMenuWnd(this);

		//m_dwStyle |= CONTROL_STYLE_TABSTOP;
		m_bExitLoop = TRUE;
		m_nSelectIndex = -1;
		m_pParentMenu = NULL;
	}

	CDUIMenu::CDUIMenu(const CDUIMenu& menu)
	{
		m_nArrowRect = menu.m_nArrowRect;
		m_nIconRect= menu.m_nIconRect;
		m_pMenuWnd = new CDUIMenuWnd(this);

		m_bExitLoop = TRUE;
		m_nSelectIndex = -1;
	}

	CDUIMenu::~CDUIMenu()
	{
		if(m_pMenuWnd != NULL)
		{
			delete m_pMenuWnd;
			m_pMenuWnd = NULL;
		}
	}

	LPVOID CDUIMenu::GetInterface(LPCTSTR strName)
	{
		if(_tcscmp(strName,DUI_CTR_MENU) == 0)
		{
			return (CDUIMenu*)this;
		}
		return theBase::GetInterface(strName);
	}



	void CDUIMenu::DoEvent(TEventUI& event)
	{
		if(!m_pMenuWnd->IsWindow())
			return;
		BOOL bHandled = FALSE;
		switch(event.Type)
		{
		case UIEVENT_MENU_CANCEL:
			if(m_pMenuWnd->IsWindow())
			{
				m_pMenuWnd->SendMessage(WM_CLOSE);
				m_bExitLoop = TRUE;
				SetSelectIndex(-1);
			}
			bHandled  = TRUE;
			break;

 		case UIEVENT_MENUITEM_MOUSEENTER:
		case UIEVENT_MENUITEM_MOUSELEAVE:
	 			OnMouseHover(event);
				bHandled  = TRUE;
	 			break;

		case UIEVENT_MENUITEM_CLICK:
			OnMouseClick(event);
			bHandled  = TRUE;
			break;

		case UIEVENT_KEYDOWN:
			OnKeyDown(event);	
			break;

		case UIEVENT_CHAR:
		//	OnChar(event);
			break;
		default:
			break;
		}

		if(bHandled) return ;

		CControlUI::DoEvent(event);
	}

	VOID CDUIMenu::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
	{
		BOOL bHandled(FALSE);

		if(_tcscmp(pstrName,_T("separate_line")) == 0)
		{
			//SetSeparateLine(GetSkinMgr()->GetImage(strValue));
			bHandled = TRUE;
		}
		else if(_tcscmp(pstrName,_T("icon_frame")) == 0)
		{
			//SetIconFrameBK(GetSkinMgr()->GetFrameBK(strValue));
			bHandled = TRUE;
		}

		else if(_tcscmp(pstrName,_T("arrowimage")) == 0)
		{
			SetArrowImage(pstrValue);
			bHandled = TRUE;
		}
		else if(_tcscmp(pstrName,_T("iconrect")) == 0)
		{
			RECT rc = { 0 };
			LPTSTR pstr = NULL;
			rc.left = _tcstol(pstrValue, &pstr, 10);  ASSERT(pstr);    
			rc.top = _tcstol(pstr + 1, &pstr, 10);    ASSERT(pstr);    
			rc.right = _tcstol(pstr + 1, &pstr, 10);  ASSERT(pstr);    
			rc.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);    
			SetIconRect(rc);
			bHandled = TRUE;
		}
		else if(_tcscmp(pstrName,_T("hottextcolor")) == 0)
		{
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetHotTextColor(clrColor);
			bHandled = TRUE;
		}
		else if(_tcscmp(pstrName,_T("arrowrect")) == 0)
		{
			RECT rc = { 0 };
			LPTSTR pstr = NULL;
			rc.left = _tcstol(pstrValue, &pstr, 10);  ASSERT(pstr);    
			rc.top = _tcstol(pstr + 1, &pstr, 10);    ASSERT(pstr);    
			rc.right = _tcstol(pstr + 1, &pstr, 10);  ASSERT(pstr);    
			rc.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);    
			SetArrowRect(rc);
			bHandled = TRUE;
		}
		else if(_tcscmp(pstrName,_T("textcolor")) == 0)
		{
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetTextColor(clrColor);
			bHandled = TRUE;
		}
		else if( _tcscmp(pstrName, _T("textpadding")) == 0 ) {
			RECT rcTextPadding = { 0 };
			LPTSTR pstr = NULL;
			rcTextPadding.left = _tcstol(pstrValue, &pstr, 10);  ASSERT(pstr);    
			rcTextPadding.top = _tcstol(pstr + 1, &pstr, 10);    ASSERT(pstr);    
			rcTextPadding.right = _tcstol(pstr + 1, &pstr, 10);  ASSERT(pstr);    
			rcTextPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);    
			SetTextPadding(rcTextPadding);
		}
		else if( _tcscmp(pstrName, _T("roundcorner")) == 0 ) {
			LPTSTR pstr = NULL;
			m_roundcorner.cx = _tcstol(pstrValue, &pstr, 10);  ASSERT(pstr);    
			m_roundcorner.cy = _tcstol(pstr + 1, &pstr, 10);    ASSERT(pstr); 
			m_pMenuWnd->GetPaintManager()->SetRoundCorner(m_roundcorner.cx,m_roundcorner.cy);
		} 
		else if(_tcscmp(pstrName,_T("menuitemhotimage")) == 0)
		{
			SetHotMenuItemImage(pstrValue);
		}
		if(!bHandled)
		{
			theBase::SetAttribute(pstrName, pstrValue);
		}
	}

	SIZE CDUIMenu::GetMenuWndSize()
	{
		SIZE sizeMenuWnd = GetMenuControlSize();
		if(m_pMenuWnd == NULL) return sizeMenuWnd;

		RECT rtFrame = {0};
	    CPaintManagerUI * pPaintMgr = m_pMenuWnd->GetPaintManager();

		RECT rtClientMargin = pPaintMgr->GetSizeBox();	
		INT nExtrWidth = rtFrame.left + rtFrame.right + rtClientMargin.left + rtClientMargin.right;
		INT nExtrHeight = rtFrame.top + rtFrame.bottom + rtClientMargin.top + rtClientMargin.bottom;
		INT nMenuWidth = sizeMenuWnd.cx + nExtrWidth;
		INT nMenuHeight = sizeMenuWnd.cy + nExtrHeight;

		sizeMenuWnd.cx += nExtrWidth;
		sizeMenuWnd.cy += nExtrHeight;

		return sizeMenuWnd;
	}

	RECT CDUIMenu::GetIdealMenuWndRect(HWND hWndParent, SIZE sizeMenuWnd, POINT ptRight, POINT ptLeft, BOOL bUseFirst)
	{
		MONITORINFO oMonitor = {0};
		oMonitor.cbSize = sizeof(oMonitor);
		::GetMonitorInfo(::MonitorFromWindow(hWndParent, MONITOR_DEFAULTTOPRIMARY), &oMonitor);
		RECT rtWork = oMonitor.rcWork;	

		BOOL bRight = TRUE;
		BOOL bDown = TRUE;

		INT nX = ptRight.x;
		INT nY = ptRight.y;
		RECT rtRect = {nX, nY, nX + sizeMenuWnd.cx, nY + sizeMenuWnd.cy};

		if(nX + sizeMenuWnd.cx > rtWork.right)
		{
			bRight = FALSE;
			if(!bUseFirst)
			{
				nX = ptLeft.x;
				nY = ptLeft.y;
			}
		}
		if(nY + sizeMenuWnd.cy > rtWork.bottom)
		{
			bDown = FALSE;
		}

		if(!bRight && bUseFirst)
		{
			if(nX - sizeMenuWnd.cx < rtWork.left)
			{
				bRight = TRUE;
				nX = rtWork.right - sizeMenuWnd.cx;
			}
		}

		if(!bDown)
		{
			if(nY - sizeMenuWnd.cy < rtWork.top)
			{
				bDown = TRUE;
				nY = rtWork.bottom - sizeMenuWnd.cy;
			}
		}

		if(bRight)
		{
			rtRect.left = nX;
			rtRect.right = nX + sizeMenuWnd.cx;
		}
		else
		{
			rtRect.right = nX;
			rtRect.left = nX - sizeMenuWnd.cx;
		}

		if(bDown)
		{
			rtRect.top = nY;
			rtRect.bottom = nY + sizeMenuWnd.cy;
		}
		else
		{
			rtRect.bottom = nY;
			rtRect.top = nY - sizeMenuWnd.cy;
		}

		return rtRect;
	}


	//bUseFirt (true ==> for first pop up menu) (false ==> for child pop up menu)
	//ptRight ==> at right side of menu item
	//ptLeft ==> left side
	BOOL CDUIMenu::ShowMenuWnd(CControlUI* pOwner, POINT ptRight, POINT ptLeft, BOOL bUseFirt/* = TRUE*/)
	{
		if(IsMemuWndPopup())
		{
			//_ASSERT(FALSE);
			return FALSE;
		}
		RECT rtMargin = {1, 1, 1, 1};

		CDUIMenuWnd *pOwerWnd;
		CDUIMenu* pControlOwner = (CDUIMenu*)pOwner->GetInterface(DUI_CTR_MENU);
		_ASSERT(pControlOwner != NULL);
		if(pControlOwner != NULL) 
		{
			pOwerWnd = pControlOwner->GetOwerWnd();
		}
		if(pOwerWnd == NULL)
		{
			_ASSERT(FALSE);
			return FALSE;
		}

		HWND hWndParent = pOwerWnd->GetPaintManager()->GetPaintWindow();
		if(GetCapture() != NULL) ::ReleaseCapture();
		if(!::IsWindow(hWndParent))
			hWndParent = NULL;
		RECT rc={0};
		m_pMenuWnd->CreateFromRootControl((CDUIMenuItem*)GetInterface(DUI_CTR_MENU),
			hWndParent /*NULL*/, rc, _T(""), WS_POPUP | WS_VISIBLE,  WS_EX_TOPMOST/* | WS_EX_TOOLWINDOW  | WS_EX_NOACTIVATE*/);

		if(m_pMenuWnd->IsWindow())
		{

			SIZE sizeMenuWnd = GetMenuWndSize();
			RECT rcInset = GetInset();
			sizeMenuWnd.cx += (rcInset.top + rcInset.bottom);
			sizeMenuWnd.cy += (rcInset.left + rcInset.right);
			RECT rtMenuWnd = GetIdealMenuWndRect(hWndParent, sizeMenuWnd, ptRight, ptLeft, bUseFirt);
			m_pMenuWnd->AdjustLocation(rtMenuWnd);

			//make parent window look like active
			if(!IsDUIMenuWnd(hWndParent))
			{
				::SendMessage(hWndParent, WM_NCACTIVATE, TRUE, 0);
			}	
			//

			m_pMenuWnd->ShowWindow();
			//m_pMenuWnd->UpdateWindow();
			return TRUE;
		}
		return FALSE;
	}
	VOID CDUIMenu::RunMenuMsgLoop()
	{
		MSG msg = {0};

		m_bExitLoop = FALSE;

		while(!m_bExitLoop)
		{
			if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);

				if(msg.message == WM_QUIT)
				{
					PostQuitMessage(0);
					break;
				}
			}

			Sleep(1);
		}
	}

	CDUIMenuItem* CDUIMenu::TrackPopupMenu(CPaintManagerUI* pParentMgr, POINT pt)
	{
		m_pMenuWnd->GetPaintManager()->UseParentResource(pParentMgr);
		if(!ShowMenuWnd(this, pt, pt, TRUE))
		{
			_ASSERT(FALSE);
			return NULL;
		}
		g_pMenuItemClicked = NULL;
		RunMenuMsgLoop();
		CDUIMenuItem* pRet = g_pMenuItemClicked;
		g_pMenuItemClicked = NULL;
		return pRet;
	}

	VOID CDUIMenu::SetIconRect(RECT rc)
	{
		m_nIconRect = rc;
	}

	RECT CDUIMenu::GetIconRect()
	{
		return m_nIconRect;
	}

	VOID CDUIMenu::SetArrowRect(RECT rc)
	{
		m_nArrowRect = rc;
	}

	RECT CDUIMenu::GetArrowRect()
	{
		return m_nArrowRect;
	}

	VOID CDUIMenu::SetArrowImage(LPCTSTR pArrowImage)
	{
		m_StrArrowImage  =pArrowImage;
	}

	LPCTSTR CDUIMenu::GetArrowImage() const
	{
	 	return (LPCTSTR)m_StrArrowImage;
 	}
	// 

	SIZE CDUIMenu::GetMenuControlSize()
	{
		SIZE sizeRet = {0};
		INT nCount = GetCount();
		bool bShowArrow = false;
		for(INT i=0; i<nCount; ++i)
		{
			CControlUI* pControl = GetItemAt(i);
			_ASSERT(pControl != NULL);
			CDUIMenuItem* pMenuItem = (CDUIMenuItem*)pControl->GetInterface(DUI_CTR_MENUITEM);
			_ASSERT(pMenuItem != NULL);
			if(pMenuItem != NULL && pMenuItem->IsVisible())
			{
				INT nItemWidth = pMenuItem->GetIdealWidth();
				if(nItemWidth > sizeRet.cx)
				{
					sizeRet.cx = nItemWidth;
				}
				sizeRet.cy += pMenuItem->GetFixedHeight();

				if(pMenuItem->GetChildMenu() != NULL)
					bShowArrow = true;
			}
		}
		if(bShowArrow)
			sizeRet.cx += GetArrowRect().right;
		return sizeRet;
	}

	VOID CDUIMenu::CancelMenu(BOOL bCancelAll /* = TRUE */)
	{
		//DoEvent(TEventUI& event)
		TEventUI evnt  = {0};
		evnt.Type = UIEVENT_MENU_CANCEL;
		evnt.pSender = this;
		if(bCancelAll)
		{
			CDUIMenu* pParent = this;
			pParent->DoEvent(evnt);
			while((pParent = pParent->GetParentMenu()) != NULL)
			{
				//pParent = pParent->GetParent();
				if(pParent->GetInterface(DUI_CTR_MENU)!=NULL)
					pParent->DoEvent(evnt);
				DUITRACE(_T("cancelMenu\r\n"));
			}

			//pParent->DoEvent(evnt);
		}
		else
		{
			DoEvent(evnt);
		}
	}

	VOID CDUIMenu::OnMouseHover(const TEventUI& event)
	{
		INT nNewIndex = -1;
		if(event.Type == UIEVENT_MENUITEM_MOUSEENTER)
		{
			nNewIndex = GetItemIndex(event.pSender);
		}
		else if(event.Type == UIEVENT_MENUITEM_MOUSELEAVE)
		{
			CDUIMenuItem *menuitem = static_cast<CDUIMenuItem*>(event.pSender->GetInterface(DUI_CTR_MENUITEM));
			if(!IsMenuItemPoupChild(menuitem))
			{
				nNewIndex = -1;
			}
			else
			{
				nNewIndex = m_nSelectIndex;
			}
		}

		if(m_nSelectIndex != nNewIndex)
		{
			SetSelectIndex(nNewIndex, TRUE);
		}
	}

	BOOL CDUIMenu::IsMenuItemPoupChild(CDUIMenuItem* pControl)
	{
		_ASSERT(pControl != NULL);
		CDUIMenuItem* pItem = (CDUIMenuItem*)pControl->GetInterface(DUI_CTR_MENUITEM);
		if(pItem == NULL)
		{
			_ASSERT(FALSE);
			return FALSE;
		}

		BOOL bPopupChild = TRUE;
		if(pItem->IsEnabled())
		{
			DWORD dwItemStyle = pItem->GetMenuItemStyle();
			if((dwItemStyle & MF_DISABLED)
				|| (dwItemStyle & MF_GRAYED)
				|| (dwItemStyle & MF_SEPARATOR)
				|| !(dwItemStyle & MF_POPUP))
			{
				bPopupChild = FALSE;
			}
		}
		if(pItem->GetChildMenu()!=NULL)
		   return bPopupChild;
		else
			return FALSE;
	}


	BOOL CDUIMenu::IsMenuItemClickable(CDUIMenuItem* pControl)
	{
		_ASSERT(pControl != NULL);
		CDUIMenuItem* pItem = (CDUIMenuItem*)pControl->GetInterface(DUI_CTR_MENUITEM);
		if(pItem == NULL)
		{
			_ASSERT(FALSE);
			return FALSE;
		}

		BOOL bEnableClick = pItem->IsEnabled();
		if(bEnableClick)
		{
			DWORD dwItemStyle = pItem->GetMenuItemStyle();
			if((dwItemStyle & MF_DISABLED)
				|| (dwItemStyle & MF_GRAYED)
				|| (dwItemStyle & MF_SEPARATOR)
				|| (dwItemStyle & MF_POPUP)
				|| !(pItem->IsVisible()))
			{
				bEnableClick = FALSE;
			}
		}

		return bEnableClick;
	}

	VOID CDUIMenu::ClickMenuItem(CDUIMenuItem* pMenuItem)
	{
		_ASSERT(pMenuItem != NULL);

		if(IsMenuItemClickable(pMenuItem))
		{
			g_pMenuItemClicked = (CDUIMenuItem*)pMenuItem->GetInterface(DUI_CTR_MENUITEM);
			CancelMenu(TRUE);
		}
	}

	VOID CDUIMenu::OnMouseClick(const TEventUI& info)
	{
		ClickMenuItem((CDUIMenuItem*)info.pSender->GetInterface(DUI_CTR_MENUITEM));
	}

	BOOL CDUIMenu::IsMenuItemSelectable(INT nIndex)
	{
		BOOL bRet(FALSE);
		CDUIMenuItem* pItem = NULL;
		pItem = (CDUIMenuItem*)GetItemAt(nIndex)->GetInterface(DUI_CTR_MENUITEM);
		_ASSERT(pItem != NULL);
		if(pItem != NULL)
		{
			DWORD dwItemStyle = pItem->GetMenuItemStyle();
			if(!(dwItemStyle&MF_SEPARATOR)
				&& pItem->IsVisible())
			{
				bRet = TRUE;			
			}
		}
		return bRet;
	}

	INT CDUIMenu::GetNextSelectIndex(BOOL bNext)
	{
		INT nIndex = m_nSelectIndex;
		INT nCount = GetCount();
		if(nCount <= 0) return -1;

		if(nIndex < 0 || nIndex >= nCount)
		{
			if(bNext) //before the first element
			{
				nIndex = -1;
			}
			else
			{
				nIndex = nCount;
			}
		}


		INT nIndexRet = -1;

		if(bNext)
		{
			for(INT i=nIndex+1; i<nCount; ++i)
			{
				if(IsMenuItemSelectable(i))
				{
					nIndexRet = i;
					break;
				}
			}

			if(nIndexRet == -1)
			{
				for(INT i=0; i<=nIndex; ++i)
				{
					if(IsMenuItemSelectable(i))
					{
						nIndexRet = i;
						break;
					}
				}
			}
		}
		else
		{
			for(INT i=nIndex-1; i>=0; --i)
			{
				if(IsMenuItemSelectable(i))
				{
					nIndexRet = i;
					break;
				}			
			}

			if(nIndexRet == -1)
			{	
				for(INT i=nCount-1; i>=nIndex; --i)
				{
					if(IsMenuItemSelectable(i))
					{
						nIndexRet = i;
						break;
					}
				}
			}
		}


		return nIndexRet;
	}


	VOID CDUIMenu::OnChar(const TEventUI& info)
	{
		TCHAR ch = info.wParam;
		if((ch >= _T('a') && ch <= _T('z'))
			|| (ch >= _T('A') && ch <= _T('Z')))
		{
			CDuiString strKey1(_T("&"));
			strKey1 += tolower(info.chKey);

			CDuiString strKey2(_T("&"));
			strKey2 += toupper(info.chKey);

			INT nCount = GetCount();
			for(INT i=0; i<nCount; ++i)
			{
				CDUIMenuItem* pItem = (CDUIMenuItem*)GetItemAt(i)->GetInterface(DUI_CTR_MENUITEM);
				_ASSERT(pItem != NULL);
				if(pItem != NULL
					&& IsMenuItemClickable(pItem))
				{
					CDuiString strText = pItem->GetText();
					INT nKey1 = strText.Find(strKey1);
					if(nKey1 != -1)
					{
						if((nKey1 == 0)
							|| (nKey1 >= 1 && (strText[nKey1-1] != _T('&'))))
						{
							ClickMenuItem(pItem);
							break;
						}
					}
					else
					{
						INT nKey2 = strText.Find(strKey2);
						if((nKey2 == 0)
							|| (nKey2 >= 1 && (strText[nKey2-1] != _T('&'))))
						{
							ClickMenuItem(pItem);
							break;
						}
					}

				}
			}
		}
	}

	VOID CDUIMenu::OnKeyDown(const TEventUI& evnet)
	{
		switch(evnet.chKey)
		{
		case VK_UP:
			{
				SetSelectIndex(GetNextSelectIndex(FALSE), FALSE);
			}
			break;


		case VK_DOWN:
			{
				SetSelectIndex(GetNextSelectIndex(TRUE), FALSE);
			}
			break;

			//	case VK_F10:
		case VK_ESCAPE:
			CancelMenu(TRUE);
			break;

		case VK_RETURN:
			{
				if(m_nSelectIndex >= 0 
					&& m_nSelectIndex < GetCount())
				{
					ClickMenuItem((CDUIMenuItem*)GetItemAt(m_nSelectIndex));
				}
			}

		case VK_RIGHT:
			{
				if(m_nSelectIndex >=0 && m_nSelectIndex < GetCount())
				{
					CDUIMenuItem* pControl = (CDUIMenuItem*)GetItemAt(m_nSelectIndex);
					if(pControl != NULL)
					{
						CDUIMenuItem* pMenuItem = (CDUIMenuItem*)pControl->GetInterface(DUI_CTR_MENUITEM);
						_ASSERT(pMenuItem != NULL);
						if(pMenuItem != NULL)
						{
							ShowChildMenu(pMenuItem, TRUE);
						}
					}
				}			
			}
			break;

		case VK_LEFT:	
			{
				CDUIMenu* pParent = GetParentMenu();
				if(pParent != NULL) 
				{
					CancelMenu(FALSE);
				}
			}
			break;
		default:	
			break;
		}
	}

	VOID CDUIMenu::SetSelectIndex(INT nIndex, BOOL bFromMouse /* = TRUE */)
	{
		if(m_nSelectIndex == nIndex) return;

		if(m_nSelectIndex != -1)
		{
			CDUIMenuItem* pControl =(CDUIMenuItem*) GetItemAt(m_nSelectIndex);
			if(pControl != NULL)
			{
				CDUIMenuItem* pMenuItem = (CDUIMenuItem*)pControl->GetInterface(DUI_CTR_MENUITEM);
				_ASSERT(pMenuItem != NULL);
				if(pMenuItem != NULL)
				{
					//pMenuItem->SetSelect(FALSE);
					pMenuItem->ModifyStatus(UISTATE_HOT,0);
					HideChildMenu(pMenuItem);
				}
			}
		}

		if(nIndex != -1)
		{
			CDUIMenuItem* pControl = (CDUIMenuItem*)GetItemAt(nIndex);
			if(pControl != NULL)
			{
				CDUIMenuItem* pMenuItem = (CDUIMenuItem*)pControl->GetInterface(DUI_CTR_MENUITEM);
				_ASSERT(pMenuItem != NULL);
				if(pMenuItem != NULL)
				{

					//pMenuItem->SetSelect(TRUE);
					pMenuItem->ModifyStatus(0,UISTATE_HOT);
					if(bFromMouse)
					{
						ShowChildMenu(pMenuItem, FALSE);
					}
				}
			}
		}

		m_nSelectIndex = nIndex;
		Invalidate();
	}

	BOOL CDUIMenu::IsMemuWndPopup()
	{
		if(m_pMenuWnd != NULL
			&& m_pMenuWnd->IsWindow())
		{
			return TRUE;
		}
		return FALSE;
	}

	VOID CDUIMenu::ShowChildMenu(CDUIMenuItem* pMenuItem, BOOL bSelectFirst /* = FALSE */)
	{
		_ASSERT(pMenuItem != NULL);
		DWORD dwItemStyle = pMenuItem->GetMenuItemStyle();
		if(!IsMenuItemPoupChild(pMenuItem)) return;
		RECT rtItem = pMenuItem->GetPos();

		CDUIMenu* pChildMenu = pMenuItem->GetChildMenu();
		if(pChildMenu == NULL) return;
		pChildMenu->SetParentMenu(this);
		_ASSERT(m_pMenuWnd != NULL && m_pMenuWnd->IsWindow());
		RECT rtMenuWnd = {0};
		m_pMenuWnd->GetWindowRect(&rtMenuWnd);

		if(!pChildMenu->IsMemuWndPopup())
		{
			POINT ptRight = {0};
			ptRight.x = rtMenuWnd.right + DUI_MENU_CHILD_GAP;
			ptRight.y = rtMenuWnd.top + rtItem.top;

			POINT ptLeft = {0};
			ptLeft.x = rtMenuWnd.left - DUI_MENU_CHILD_GAP;
			ptLeft.y = ptRight.y;
			pChildMenu->ShowMenuWnd(this, ptRight, ptLeft, FALSE);
		}

		if(bSelectFirst)
		{
			pChildMenu->SetSelectIndex(pChildMenu->GetNextSelectIndex(TRUE), FALSE);
		}
	}

	VOID CDUIMenu::HideChildMenu(CDUIMenuItem* pMenuItem)
	{
		_ASSERT(pMenuItem != NULL);
		DWORD dwItemStyle = pMenuItem->GetMenuItemStyle();
		if(!IsMenuItemPoupChild(pMenuItem)) return;

		CDUIMenu* pChildMenu = pMenuItem->GetChildMenu();
		if(pChildMenu == NULL) return;

		if(m_pMenuWnd != NULL
			&& m_pMenuWnd->IsWindow())
		{
			((CDUIMenu*)pChildMenu)->CancelMenu(FALSE);
		}
	}



	CDUIMenuWnd* CDUIMenu::GetOwerWnd() const
	{
		return m_pMenuWnd;
	}

	void CDUIMenu::SetTextColor( DWORD _TextColor )
	{
		m_dwTextColor	= _TextColor;	
	}

	DWORD CDUIMenu::GetTextColor() const
	{
		return m_dwTextColor;
	}

	void CDUIMenu::SetTextPadding( RECT rc )
	{
		m_rcTextPadding = rc;
	}

	RECT CDUIMenu::GetTextPadding() const
	{
		return m_rcTextPadding;
	}

	void CDUIMenu::DoPaint( HDC hDC, const RECT& rcPaint )
	{
		theBase::DoPaint(hDC,rcPaint);
	}

	void CDUIMenu::SetHotMenuItemImage( LPCTSTR pStrImage )
	{
		m_sHotImage = pStrImage;
	}

	LPCTSTR CDUIMenu::GetHotMenuItemImage() const
	{
		return (LPCTSTR)m_sHotImage;
	}

	void CDUIMenu::SetHotTextColor( DWORD TextColor )
	{
		m_dwHotTextColor = TextColor;
	}

	DWORD CDUIMenu::GetHotTextColor() const
	{
		return m_dwHotTextColor;
	}

	void CDUIMenu::SetParentMenu(CDUIMenu * pParentMenu )
	{
		m_pParentMenu = pParentMenu ;
	}

	CDUIMenu * CDUIMenu::GetParentMenu() const
	{
		return m_pParentMenu;
	}

	////////////////////  CDUIMenu  /////////////////////////
}